﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace DoAnOOP.DataAccessLayer
{
    class clsConnect
    {
        #region Biến 
        public  static SqlConnection myConnect;
        private SqlCommand com;
        private SqlDataAdapter daaAp;
        private DataTable dataTable;

        public string Server { get; set; }
        public string  Database { get; set; }
        public string User { get; set; }
        public string  Password { get; set; }

        public static string ConnectionString;

        public clsConnect()
        {
            myConnect = new SqlConnection();
            com = new SqlCommand();
            daaAp = new SqlDataAdapter();
        }
        #endregion

        #region Chuỗi kết nối
        public void ReadSource()
        {
            StreamReader reader = new StreamReader("ConnectString.con");
            this.Server = reader.ReadLine().Split(':')[1];
            this.Database = reader.ReadLine().Split(':')[1];
            this.User = reader.ReadLine().Split(':')[1];
            this.Password = reader.ReadLine().Split(':')[1];
            ConnectionString = GetConnectString();
            reader.Close();
        }
        public void WriterFile(string server, string database, string uid, string pass)
        {
            StreamWriter writer = new StreamWriter("ConnectString.con");
            writer.WriteLine("Server: " + server);
            writer.WriteLine("Database: " + database);
            writer.WriteLine("User: " + uid);
            writer.WriteLine("Password: " + pass);
            writer.Close();
        }
        public string GetConnectString()
        {
            if (this.User == "")
                return "Data Source = " + this.Server + "; Initial Catalog = " + this.Database + "; User ID = " + this.User + "; Password = " + this.Password + ";";
            else
                return "Data Source = " + this.Server + "; Initial Catalog =" + this.Database + "; Integrated Security = True" + ";";
               
        }
        #endregion

        #region Kết nối
        public void Connect()
        {
            try
            {
                myConnect.ConnectionString = GetConnectString();
                myConnect.Open();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static SqlConnection GetSqlConnect()
        {
            SqlConnection myConnect = new SqlConnection(ConnectionString);
            myConnect.Open();
            return myConnect;
        }
        #endregion 

        #region Câu truy vấn command
        public void ExecuteNonquery(string sql)
        {
            SqlCommand command = new SqlCommand(sql, clsConnect.GetSqlConnect());
            try
            {
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region Lấy dữ liệu
        public DataTable GetDataTable(string sql)
        {
            ReadSource();
            SqlConnection con = new SqlConnection(GetConnectString());
            daaAp = new SqlDataAdapter(sql, clsConnect.GetSqlConnect());
            dataTable = new DataTable();
            daaAp.Fill(dataTable);
            return dataTable;
        }
        #endregion

    }
    
}
